/*
 * @Description: 机型相关的api
 */
const device={
  // 获取安卓版本号
  getAndroidVersion:function() {
    let ua =navigator.userAgent.toLowerCase() 
    let match = ua.match(/android\s([0-9\.]*)/)
    return match ? match[1] : false
  },
  // 获取ios版本号
  getIosVersion:function (){
    let str= navigator.userAgent.toLowerCase() 
    let ver=str.match(/cpu iphone os (.*?) like mac os/)
    return ver ? ver[1]:false
  },
  isAndroid () {
    const ua = navigator.userAgent
    return /Android/i.test(ua)
  },
  getPhoneVersion(){
    let version= device.getAndroidVersion() || device.getIosVersion()
    if (!version) return null
    let result= version.replace(/\_/g,'.')
    console.log(`系统版本：【${result}】`)
    return result
  },
  async getAppVersion(){
    return new Promise(function(resolve,reject){
      try {
        window.App.getAppVersion(res => {
          if (res.flag==='S'){
            return resolve(res)
          } else {
            reject(res.msg)
          }
        })
      } catch (e){
        reject(e)
      }  
    })
  },
  async getRuleDeviceParams(){
    let info = await device.getAppVersion()
    let para
    if (device.isAndroid()){
      para= {
        iosApp:'',
        iosOs:'',
        androidApp:info.data.version,
        androidOs:device.getPhoneVersion()
      }
    } else {
      para= {
        iosApp:info.data.version,
        iosOs:device.getPhoneVersion(),
        androidApp:'',
        androidOs:''
      }
    }
    console.log('【系统参数】')
    console.log(JSON.stringify(para))
    return para
  }
}
export default device